$(function () {
    $(".loadingMarsk").css({
        "width":window.screen.availWidth,
        "height":window.screen.availHeight,
    });
    //获取token
    var token = localStorage.token;
    if(!token) {
        location.href = "../loginAndReg/login.html"
    }
    new FastClick(document.body);
    //加载地址列表
    $.ajax({
        type: "get",
        url: domain.test + "/getaddresslist",
        // url: domain.online + "/address/list",
        beforeSend: function (xhr) {
            $(".loadingMarsk").css("display","block");            
            xhr.setRequestHeader("token", token);
        },
        success: function (response) {
            if (response.data.rtnCode === 200) {
            // if (response.rtnCode === 200) {
                var addressList = response.data.data;
                // var addressList = response.data;
                $(".addressList").html(template("addressList", { addressList: addressList }));
                $(".loadingMarsk").css("display","none");
            }
            if (response.data.rtnCode === 401 || response.data.rtnCode === 400) {
            // if (response.rtnCode === 401 || response.rtnCode === 400) {
                var outTimeCode = response.data.rtnCode;
                // var outTimeCode = response.rtnCode;
                location.href = "../../index.html?outTimeCode=" + outTimeCode;
            }
        }
    });
    //设置默认地址
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    $(".addressList").on("click", ".adminAdd", function (e) {
        var _this = $(this);
        e.stopPropagation();
        var status = parseInt($(this).find(".hidden").val());
        if (status === 1) {
            return false;
        }
        var addressId = $(this).parent().parent().attr("data-addressId");
        var regionId = $(this).parent().parent().attr("data-regionId");
        var contact = $(this).parent().parent().find(".person").attr("data-contcat");
        var mobile = $(this).parent().parent().find(".tel").attr("data-mobile");
        var address = $(this).parent().parent().find(".address").attr("data-address");
        var region = $(this).parent().parent().find(".address").attr("data-region");
        $.ajax({
            type: "post",
            url: domain.test + "/address",
            // url: domain.online + "/address/save",
            data: {
                id: addressId,
                contact: contact,
                mobile: mobile,
                address: address,
                regionId: regionId,
                default: 1
            },
            beforeSend: function (xhr) {
                $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");                
                $(".loadingMarsk").css("display","block");                
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                // if (response.rtnCode === 200) {
                    $(".loadingMarsk").css("display","none");   
                    // tips("设置成功，正在为你跳转");           
                    // location.reload();
                    $("label").find("img").attr("src","../../static/img/ico_selectk@3x.png");
                    $(".hidden").val(0);
                    _this.find("label").find("img").attr("src","../../static/img/ico_select@3x.png");
                    _this.find(".hidden").val(1);
                }
                if (response.data.rtnCode === 401 || response.data.rtnCode === 400) {
                // if (response.rtnCode === 401 || response.rtnCode === 400) {
                    var outTimeCode = response.data.rtnCode;
                    // var outTimeCode = response.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    });
    //删除地址
    var regionId;
    $(".addressList").on("click",".del",function() {
        regionId =  $(this).parent().parent().parent().attr("data-addressId");
        var width = window.screen.width;
        var height = window.screen.height;
        $(".logoutMarsk").css({
            "width":width,
            "height":height,
            "display":"block"
        });
    });
    //取消删除
    $(".cancel").on("click",function() {
        $(".logoutMarsk").css("display","none");
    });
    $(".sure").on("click",function() {
        $.ajax({
            type: "delete",
            url: domain.test + `/deladdress?id=${regionId}`,
            // url: domain.online + `/address/del?id=${regionId}`,
            beforeSend: function(xhr) {
                $(".logoutMarsk").css("display","none");
                $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");                
                $(".loadingMarsk").css("display","block");   
                xhr.setRequestHeader("token",token);
            },
            success: function (response) {
                if(response.data.rtnCode===200) {
                // if(response.rtnCode===200) {
                    $(".loadingMarsk").css("display","none");
                    // $.each($(".addressItem"),function(index,value) {
                    //     if($(value).attr("data-addressId")===regionId) {
                    //         value.remove();
                    //     }
                    // })  
                    location.reload();
                }
            }
        });
    });
    //编辑地址
    $('.addressList').on("click",".edit",function(e) {
        e.stopPropagation();
        var status = parseInt($(this).parent().parent().find(".hidden").val());
        var addressId = $(this).parent().parent().parent().attr("data-addressId");
        var regionId = $(this).parent().parent().parent().attr("data-regionId");
        var contact = $(this).parent().parent().parent().find(".person").attr("data-contcat");
        var mobile = $(this).parent().parent().parent().find(".tel").attr("data-mobile");
        var address = $(this).parent().parent().parent().find(".address").attr("data-address");
        var region = $(this).parent().parent().parent().find(".address").attr("data-region");
        localStorage.editStatus = status;
        localStorage.editAddressId = addressId;
        localStorage.editRegionId = regionId;
        localStorage.editContact = contact;
        localStorage.editMobile = mobile;
        localStorage.editAddress = address;
        localStorage.editRegion = region;
        localStorage.editAddressStatus = 1;        
        location.href = "./addAddress.html";
    });
    //添加地址跳转
    $(".addAdressBtn").on("click",function(){
        localStorage.editStatus = "";
        localStorage.editAddressId = "";
        localStorage.editRegionId = "";
        localStorage.editContact = "";
        localStorage.editMobile = "";
        localStorage.editAddress = "";
        localStorage.editRegion = "";
        localStorage.addAddressStatus = 1;
        location.href = "./addAddress.html"
    });
    //确认订单选择地址跳转
    $(".addressList").on("click",".addressItem",function() {
        var choseAddressStatus = parseInt(localStorage.choseAddressStatus);
        if(choseAddressStatus===1) {
            var addressId = $(this).attr("data-addressId");
            var contact = $(this).find(".person").attr("data-contcat");
            var mobile = $(this).find(".tel").attr("data-mobile");
            var address = $(this).find(".address").attr("data-region") + " " +  $(this).find(".address").attr("data-address");
            var catchAdress = {};
            catchAdress.addressId = addressId;
            catchAdress.contact = contact;
            catchAdress.mobile = mobile;
            catchAdress.address = address;
            localStorage.catchAdress = JSON.stringify(catchAdress);
            location.href = "./sureOrder.html"
        }
    });
});